package com.webbdong.aop.agent.test.bytebuddy;

import com.webbdong.aop.agent.test.bean.Dog;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.pool.TypePool;

import static net.bytebuddy.matcher.ElementMatchers.named;

/**
 * @program: spring-agent
 * @description:
 * @author: zt22328 赵儒定
 * @create: 2023-03-30 09:18
 **/
public class DynamicType2 {

    public static void main(String[] args) throws Exception {
        TypeDescription templateTypeDescription = TypePool.Default.of(BytebuddyTest.class.getClassLoader())
                .describe("com.webbdong.aop.agent.test.ConstructorInterTemplate").resolve();

        Class<?> loaded = new ByteBuddy()
                .redefine(templateTypeDescription, ClassFileLocator.ForClassLoader
                        .of(BytebuddyTest.class.getClassLoader()))
                .name("InternalInterceptorName_test")
                .field(named("TARGET_INTERCEPTOR"))
                .value("com.webbdong.aop.agent.test.ConstructorInterceptor")
                .make()
                .load(BytebuddyTest.class.getClassLoader()).getLoaded();
//                .saveIn(new File("E:\\gitee\\spring-agent\\aop-agent-test\\target"));

        Object object = loaded.newInstance();
        System.out.println(object.getClass().getDeclaredField("TARGET_INTERCEPTOR").get(object));
        object.getClass().getDeclaredMethods()[1].invoke(object, new Dog(), null);

        System.out.println(object.getClass().getDeclaredMethods()[1]);

        System.out.println(object.getClass().getDeclaredMethods().length);

    }
}
